package com.airlines.basedata.controller;

import com.airlines.basedata.domain.Aircraft;
import com.airlines.basedata.service.AircraftService;
import com.airlines.common.Page;
import com.airlines.common.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 描述：飞机信息表Controller
 * @author wangkang
 * DATE 2018/9/17
 */
@Controller
@RequestMapping("/aircraft")
public class AircraftController {
    private static final String PREFIX = "/basedata";
    @Autowired
    private AircraftService aircraftService;

    /**
     * 进入飞机信息页面
     * @return
     */
    @RequestMapping(value = "",method = RequestMethod.GET)
    public String toAirarcftList(){
        return PREFIX + "/aircraftList";
    }

    /**
     * 多条件模糊查询，带分页
     * @param aircraft
     * @param offset
     * @param limit
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Page toAircraftList(Aircraft aircraft,String offset, String limit){
        Page page = new Page();
        page.setPageNumber((Integer.valueOf(offset) / (Integer.valueOf(limit))));
        page.setPageSize(Integer.valueOf(limit));
        page = aircraftService.listData(aircraft,page);
        return page;
    }

    /**
     * 根据机号层查询，进入修改界面
     * @param model
     * @param acReg
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public String toEditAircraPage(Model model,String acReg){
        Aircraft aircraft = aircraftService.getByPrimaryKey(acReg);
        model.addAttribute("aircraft",aircraft);
        return PREFIX + "aircraftEdit";
    }

    /**
     * 保存修改信息
     * @param aircraft
     * @return
     */
    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ResponseBody
    public R updateAircra(Aircraft aircraft){
        int row = aircraftService.updateByPrimaryKeySelective(aircraft);
        if(row>0){
            return R.ok();
        }
        return R.error();
    }
}
